package com.chunxuan.langquan.ui.activity;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.Base2Activity;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.ActiviceCollector;
import com.chunxuan.langquan.support.util.Logg;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.HandlerUtil;
import com.ruitu.arad.util.ToastUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class BindMobileActivity extends Base2Activity implements HandlerUtil.OnReceiveMessageListener{
    private AppCompatEditText et_phone, et_code;// 手机号，验证码
    private TextView tv_send_code, tv_ok, tv_privacy_policy, tv_service_agreement;// 发送验证码,注册，隐私政策，协议条款
//    private CheckBox cb_check;

    private String phone, code;
    private boolean isCd = false;// 是否正在倒计时
    private int currSecond = 60;// 当前倒计时的秒数
//
    private HandlerUtil.HandlerHolder handler = new HandlerUtil.HandlerHolder(this);

    @Override
    public void handlerMessage(Message msg) {
        if (msg.what == 20101651) {
            currSecond--;
            if (currSecond <= 0) {
                tv_send_code.setText("获取验证码");
                currSecond = 60;
                isCd = false;// 取消倒计时状态
            } else {
                tv_send_code.setText(currSecond + "秒");
                handler.sendEmptyMessageDelayed(20101651, 1000);
            }
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_bind_mobile;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        et_phone = findViewById(R.id.et_1);
        et_code = findViewById(R.id.et_2);
        tv_send_code = findViewById(R.id.tv_send_sms);
        tv_privacy_policy = findViewById(R.id.tv_privacy_policy);
        tv_service_agreement = findViewById(R.id.tv_service_agreement);
        tv_ok = findViewById(R.id.tv_ok);
//        cb_check = findViewById(R.id.cb_check);
        setHeadTitle("");
        setOnClickListener(tv_send_code, tv_ok, tv_privacy_policy, tv_service_agreement);
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_send_sms:
                if (!isCd) {
                    phone = et_phone.getText().toString().trim();
                    if (TextUtils.isEmpty(phone)) {
                        ToastUtils.showShort("请输入手机号");
                        return;
                    }
                    getMsgCode();
                    showProgress();
                    isCd = true;
                    handler.sendEmptyMessage(20101651);
                }
                break;
            case R.id.tv_ok:
//                Intent intent=new Intent(this, SelectCityActivity.class);
//                startActivity(intent);
//                if (!cb_check.isChecked()) {
//                    ToastUtils.showShort("请阅读并同意《用户服务协议》");
//                    return;
//                }
                phone = et_phone.getText().toString().trim();
                code = et_code.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtils.showShort("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showShort("请输入验证码");
                    return;
                }
//                register();
                showProgress();
                break;
            case R.id.tv_privacy_policy:

                break;
            case R.id.tv_service_agreement:

                break;
        }
    }

    public Disposable getMsgCode() {
        return APIRetrofit.getDefault().getMsgCode(phone,"mobilelogin")
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 baseResult2) throws Exception {
                        hideProgress();
                        Logg.e("==发送短信验证码== " + baseResult2);
                        if (baseResult2.isSuccess()) {
                            ToastUtils.showShort(baseResult2.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("发送失败，请重试");
                    }
                });
    }

//    public Disposable register() {
//        return APIRetrofit.getDefault().register(phone, pwd,confirmPwd,code)
//                .compose(RxUtil.<BaseResult2>subIoObMain())
//                .subscribe(new Consumer<BaseResult2>() {
//                    @Override
//                    public void accept(BaseResult2 baseResult2) throws Exception {
//                        hideProgress();
//                        Logg.e("==注册账号== " + baseResult2);
//                        if (baseResult2.isSuccess()) {
//                            ToastUtils.showShort(baseResult2.getMsg());
//                            finish();
//                        } else {
//                            ToastUtils.showShort(baseResult2.getMsg());
////                            if (TextUtils.equals("4001", baseResult2.getCode())) {// 注册成功,完善信息
////                                LinkedTreeMap<String, Integer> user = (LinkedTreeMap) baseResult2.getData();
////                                String tmp = user.get("id") + "";
////                                if (tmp.contains(".")) {
////                                    Global.USER_ID = tmp.substring(0, tmp.lastIndexOf("."));
////                                } else {
////                                    Global.USER_ID = tmp;
////                                }
////                                Logg.i("Global.USER_ID = " + Global.USER_ID);
////                                startActivity(RealAuthAgentActivity.class);
////                            }
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        hideProgress();
//                        throwable.printStackTrace();
//                        ToastUtils.showShort("注册失败，请重试");
//                    }
//                });
//    }

}
